//
//  CXYMenuView.swift
//  诚叙缘
//
//  Created by sujeking on 17/4/12.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit

protocol CXYMenuViewDelegate {
    func selectGatergoryItem(index:Int)-> Void
}


class CXYMenuView: UIView {
    
    @IBOutlet weak var lineView: UIView!
    @IBOutlet weak var lineLeadingConstraint: NSLayoutConstraint!
    open var delegate:CXYMenuViewDelegate?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
    @IBAction func flowerBtnClick(_ sender: Any) {
        animationForIndex(index: 0)
    }
    
    @IBAction func qiaokeliBtnClick(_ sender: Any) {
        animationForIndex(index: 1)
    }
    
    @IBAction func wawaBtnClick(_ sender: Any) {
        animationForIndex(index: 2)
    }
    
    @IBAction func peishiBtnClick(_ sender: Any) {
        animationForIndex(index: 3)
    }

    @IBAction func packageBtnClick(_ sender: Any) {
        animationForIndex(index: 4)
    }
    
    func animationForIndex(index:Int) {
        let lineWidth = self.lineView.bounds.width
        let postionx = lineWidth * CGFloat(index)
        
        lineLeadingConstraint.constant = postionx
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.1, options: UIViewAnimationOptions(rawValue: 0), animations: {
            self.layoutIfNeeded()
        }) { (Bool) in
            if self.delegate != nil {
                self.delegate?.selectGatergoryItem(index: index)
            }
        }
    }
}
